require(['./config'],()=>{
    require(['template','footer','header'],(template,footer)=>{
        class Cart{
            constructor(){
             this.init()
           
            }
            init(){
                let cartList=localStorage.getItem('cart')
                this.cartList=JSON.parse(cartList)

                console.log(cartList.length)
                if(cartList.length!=2){
                    
                    $('#shopping_car').html(template('srcShop',{cartList:this.cartList}))
                    this. calcTotalMoney()
                    //给所有的单选绑事件
                    this.checksChange()
                    //删除数据
                    this.checKQuan()
                    this.checkMin()
                    this.checkAdd()
                    this.allcheck()
                   
                }else if(cartList.length==2){
                   
                    $('.empty').show()
                }
            }
            calcTotalMoney(){
              $('#Paymoney').html(this.cartList.reduce((money,cart)=>{
                  if(cart.check) money +=cart.price * cart.num
                  return money
              },0).toFixed(2))
            }
            checksChange(){
                const _this=this
                $('#shopping_car .shop_cont input:checkbox').on('change',function(){
                    let id=$(this).parents('tr').attr('data-id')
                    _this.cartList=_this.cartList.map(cart=>{
                        if(cart.id==id) cart.check=$(this).prop('checked')
                        return cart
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cartList))
                    //全选按钮是根据数据是否全旋来渲染的
                   $('#all-check').prop('checked',_this.cartList.every(cart=>{
                       return cart.check
                   }))
                   //计算总价
                   _this.calcTotalMoney()
                  
                })
            }
         checKQuan () {
              const _this=this
              $('.shanchu').on('click',function(){
                let id=$(this).parents('tr').attr('data-id')
                _this.cartList = _this.cartList.filter(cart=>{
                    return (cart.id!=id)
                })
                localStorage.setItem('cart',JSON.stringify(_this.cartList))
                $(this).parents('tr').remove()
                console.log(cart)
                let cart=localStorage.getItem('cart')
                //全选按钮是根据数据是否全选来渲染的
                if (_this.cartList.length==0) {
                    
                    $('table').remove()
                    $('.empty').show()
                }
                footer.calcCartNum()
                
               //计算总价
               _this.calcTotalMoney()
               
              })
             }
             checkMin(){
                 const _this=this
                 $('.minus').on('click', function(){
                     var num=$(this).next('.shop-id').val()
                     
                     num--
                     if(num<1){
                       num=1
                     }
                     $(this).next('.shop-id').val(num)
                     const id=$(this).parents('tr').attr('data-id')
                     _this.cartList=_this.cartList.map(cart=>{
                         if(cart.id==id){
                             cart.num=num
                             this.prode=cart.num*cart.price

                         }
                         return cart
                     })
                     localStorage.setItem('cart',JSON.stringify(_this.cartList))
                     this.prode=this.prode
                    $(this).parents('tr').siblings('.prode').html('￥'+ this.prode)
                    footer.calcCartNum()
                    _this. calcTotalMoney()
                 })
                
             }
             checkAdd(){
                const _this=this
                $('.plus').on('click', function(){
                    var num=$(this).prev('.shop-id').val()
                    
                    num++
                    $(this).prev('.shop-id').val(num)
                    const id=$(this).parents('tr').attr('data-id')
                  
                    _this.cartList=_this.cartList.map(cart=>{
                        if(cart.id==id){
                            cart.num=num
                            this.prode=cart.num*cart.price
                            length
                        }
                        return cart
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cartList))
                    this.prode=this.prode
                   $(this).parents('tr').siblings('.prode').html('￥'+ this.prode)
                   footer.calcCartNum()
                   _this. calcTotalMoney()
                })
               
            }
            allcheck () {
                const _this = this;
                $('#all-check').on('change', function() {
                    _this.cartList = _this.cartList.map(cart => {
                        cart.check = $('#all-check').prop('checked')
                        return cart;
                    })
                    localStorage.setItem('cart', JSON.stringify(_this.cartList))
                    var check = _this.cartList.every(cart => {
                        return cart.check
                    })
                    if (check) {
                        $('input:checkbox').prop('checked', true)
                       
                    } else {
                        $('input:checkbox').prop('checked', false)
                    }
                    _this. calcTotalMoney()
                })
            }
          
        }
        new Cart()
    })
})